﻿namespace ApiTemplate.Common
{
    /// <summary>
    /// 字符串相关扩展方法
    /// </summary>
    public static class StringExtension
    {
        /// <summary>
        /// 为空或者字符串(已经去除空格了)
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string s)
        {
            return string.IsNullOrEmpty(s?.Trim());
        }

        /// <summary>
        /// 不为空或者字符串(已经去除空格了)
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsNotNullOrEmpty(this string s)
        {
            return !string.IsNullOrEmpty(s?.Trim());
        }

        #region 类型转换
        /// <summary>
        /// 转换成数字类型
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static int ToInt32(this string s)
        {
            int.TryParse(s, out int result);
            return result;
        }

        /// <summary>
        /// 转为小数
        /// </summary>
        /// <param name="strValue"></param>
        /// <returns></returns>
        public static float ToFloat(this string strValue)
        {
            float.TryParse(strValue, out float result);
            return result;
        }

        /// <summary>
        /// 转为小数
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static decimal ToDecimal(this string s)
        {
            decimal.TryParse(s, out decimal result);
            return result;
        }

        /// <summary>
        /// 转为小数
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static double ToDouble(this string s)
        {
            double.TryParse(s, out double result);
            return result;
        }

        /// <summary>
        /// 转为日期类型
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this string s)
        {
            DateTime.TryParse(s, out DateTime result);
            return result;
        }
        #endregion

        #region 字符串加密

        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string AESEncrypt(this string s)
        {
            return EncryptionHelper.AESEncrypt(s);
        }

        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string AESDecrypt(this string s)
        {
            return EncryptionHelper.AESDecrypt(s);
        }

        /// <summary>
        /// Base64加密
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string StringToBase64(this string s)
        {
            return EncryptionHelper.Base64Encrypt(s);
        }

        /// <summary>
        /// Base64解密
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string Base64ToString(this string s)
        {
            return EncryptionHelper.Base64Decrypt(s);
        }

        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string DesEncrypt(this string s)
        {
            return EncryptionHelper.DesEncrypt(s);
        }

        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string DesDecrypt(this string s)
        {
            return EncryptionHelper.DesDecrypt(s);
        }

        /// <summary>
        /// Md5加密
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string Md5(this string s)
        {
            return EncryptionHelper.Md5(s);
        }
        #endregion

        #region Http请求

        /// <summary>
        /// GET请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="timeout">超时时间（单位：秒）：默认超时时间30秒</param>
        /// <returns>返回类型为元祖，第一个参数为：请求结果；第二个参数为是否请求成功</returns>
        public static async Task<Tuple<string, bool>> GetAsync(this string url, int timeout = 30)
        {
            return await HttpHelper.GetAsync(url, timeout);
        }

        /// <summary>
        /// POST请求
        /// </summary>
        /// <param name="url">请求的url</param>
        /// /// <param name="data">参数：json格式</param>
        /// <param name="timeout">超时时间（单位：秒）：默认超时时间30秒</param>
        /// <returns>返回类型为元祖，第一个参数为：请求结果；第二个参数为是否请求成功</returns>
        public static async Task<Tuple<string, bool>> PostAsync(this string url, string data, int timeout = 30)
        {
            return await HttpHelper.PostAsync(url, data, timeout);
        }
        #endregion
    }
}
